<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎━(*｀∀´*)ノ亻!来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="default"
              class="form_btn"
              @click="userLogin"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/stores/modules/user'
//引入路由
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time.ts'
//获取路由对象
import { useRoute } from 'vue-router'
const route = useRoute()
//创建路由对象
const router = useRouter()
const userStore = useUserStore()
const loginForm = reactive({
  username: 'admin',
  password: '111111',
})
const loading = ref(false)
const validatorUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}
const validatorPssword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少六位'))
  }
}
const rules = {
  username: [{ trigger: 'change', validator: validatorUserName }],
  password: [{ trigger: 'change', validator: validatorPssword }],
}

const loginForms = ref()
const userLogin = async () => {
  await loginForms.value.validate()

  //点击登录按钮，开始加载
  loading.value = true
  try {
    await userStore.userLogin(loginForm)
    //判断登录的时候路径当中是否有query参数，如果有就往query参数跳转，没有就调往首页
    const redirect: any = route.query.redirect
    // console.log(redirect);
    // /product/attr
    router.push({ path: redirect || '/' })
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `${getTime()}`,
    })
    //加载效果消失
    loading.value = false
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url('@/assets/images/background.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  .login_form {
    padding: 40px;
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png');
  }
  h1 {
    color: white;
    font-size: 40px;
  }
  h2 {
    margin: 20px 0px;
    color: white;
    font-size: 20px;
  }
  .form_btn {
    width: 100%;
  }
}
</style>
